﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using MiniCrawler.DataAccess.Repositories;
using MiniCrawler.Entity;
using MiniCrawler.Logging;

namespace MiniCrawler.Business.Collectors.ResourceCollectors
{
    public class ModClothArranger
    {
        public string hostName;
        public List<DownloadLink> lstDownloadLinks;

        public void Run()
        {
            try
            {

                string resourcePath = "C:\\Users\\Hoang Anh\\Desktop\\NastyGal\\Image\\";
                string destinationPath = "C:\\Users\\Hoang Anh\\Desktop\\NastyGal\\Sort\\";

                //var fileList = Directory.GetFiles(resourcePath);
                //var lstDownloadLinks = DownloadLinkHelper.GetUndownloaded(hostName);


                var filePath = string.Empty;
                var fileName = string.Empty;

                for (int i = 0; i < lstDownloadLinks.Count(); i++)
                {

                    DownloadLink link = lstDownloadLinks[i];
                    fileName = Path.GetFileNameWithoutExtension(link.Value) + "_2" + Path.GetExtension(link.Value);
                    filePath = resourcePath + fileName;

                    if (link != null)
                    {
                        string folder = link.Path;
                        folder = destinationPath + folder.Replace("-gt;", "\\");

                        //Log.WriteLog("[Create Folder][" + folder + "]");
                        Directory.CreateDirectory(folder);

                        //Log.WriteLog("[Move file][" + fileName + "]");

                        if (!File.Exists(folder + "\\" + fileName) && File.Exists(filePath))
                            File.Move(filePath, folder + "\\" + fileName);
                        else
                        {
                            if (File.Exists(filePath))
                                File.Delete(filePath);
                        }
                        //Log.WriteLog("[Update file][" + fileName + "]");
                        Log.WriteLog("[Remain][" + i + "/" + (lstDownloadLinks.Count() - i) + "]");

                        link.IsDownloaded = true;
                        link.State = 3;
                        DownloadLinkHelper.UpdateLink(link);


                    }
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
            }
        }

        public void RunGetData()
        {
            try
            {

                string destinationPath = "C:\\Users\\Hoang Anh\\Desktop\\NastyGal\\Sort\\";

                //var fileList = Directory.GetFiles(resourcePath);
                //var lstDownloadLinks = DownloadLinkHelper.GetUndownloaded(hostName);


                var filePath = string.Empty;
                var fileName = string.Empty;

                for (int i = 0; i < lstDownloadLinks.Count(); i++)
                {

                    DownloadLink link = lstDownloadLinks[i];
                    fileName = "url.txt";

                    if (link != null)
                    {

                        string folder = link.Path;
                        folder = destinationPath + folder.Replace("-gt;", "\\");

                        //Log.WriteLog("[Create Folder][" + folder + "]");
                        Directory.CreateDirectory(folder);

                        //Log.WriteLog("[Move file][" + fileName + "]");

                        if (!File.Exists(folder + "\\" + fileName))
                            File.WriteAllText(folder + "\\" + fileName, link.Url);
                        else
                        {
                            File.Delete(folder + "\\" + fileName);
                            File.WriteAllText(folder + "\\" + fileName, link.Url);
                        }
                        //Log.WriteLog("[Update file][" + fileName + "]");
                        Log.WriteLog("[Remain][" + i + "/" + (lstDownloadLinks.Count() - i) + "]");

                    }
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
            }
        }
    }
}
